package com.tools.scanner.ad.impl

import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdLoader
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.nativead.NativeAd
import com.google.android.gms.ads.nativead.NativeAdOptions
import com.google.android.gms.ads.nativead.NativeAdView
import com.tools.scanner.QrApp
import com.tools.scanner.R

class NativeAdImpl(mAdId: String, mLocation: String) : BaseAdImpl(mAdId, mLocation) {

    private var mNativeAd: NativeAd? = null
    private var loadTime: Long = 0L

    override fun beginLoad(adLocation: String, adId: String) {
        val adLoader = AdLoader.Builder(QrApp.app, adId)
            .forNativeAd(object : NativeAd.OnNativeAdLoadedListener {
                override fun onNativeAdLoaded(nativeAd: NativeAd) {
                    nativeAd.setOnPaidEventListener { adValue ->
                        notifyAdPaid(adValue)
                    }

                    mNativeAd = nativeAd
                    loadTime = System.currentTimeMillis()
                    val adapterName =
                        nativeAd.responseInfo?.mediationAdapterClassName ?: ""
                    notifyAdLoadEnd(-1, "", adapterName, true)
                }
            })
            .withAdListener(object : AdListener() {
                override fun onAdImpression() {
                    super.onAdImpression()
                    notifyAdShow()
                }

                override fun onAdFailedToLoad(loadAdError: LoadAdError) {
                    val errorCode = loadAdError.code
                    val errorMessage = loadAdError.message
                    notifyAdLoadEnd(errorCode, errorMessage, "", false)
                }

                override fun onAdClicked() {
                    mNativeAd = null
                    notifyClick()
                }
            })
            .withNativeAdOptions(NativeAdOptions.Builder().build())
            .build()
        adLoader.loadAd(AdRequest.Builder().build())
    }

    override fun beginShow(activity: Activity, adLocation: String, adId: String) {

    }

    override fun getNativeView(): View? {
        val currentAd = mNativeAd ?: return null

        val adView =
            LayoutInflater.from(QrApp.app).inflate(R.layout.native_ad, null) as NativeAdView
        adView.findViewById<TextView>(R.id.title).apply {
            text = currentAd.headline
            adView.headlineView = this
        }
        adView.findViewById<TextView>(R.id.content).apply {
            text = currentAd.body
            adView.bodyView = this
        }
        adView.findViewById<TextView>(R.id.button).apply {
            text = currentAd.callToAction
            adView.callToActionView = this
        }

        currentAd.icon?.drawable?.let {
            val iconView = adView.findViewById<ImageView>(R.id.icon)
            iconView.setImageDrawable(it)
            adView.iconView = iconView
        }

        adView.setNativeAd(currentAd)

        return adView
    }

    override fun isAdAvailable(): Boolean {
        if (mNativeAd == null) {
            return false
        }

        if (System.currentTimeMillis() - loadTime < 1800000) {
            return true
        }

        mNativeAd?.destroy()
        mNativeAd = null
        return false
    }
}